package com.mybatis_demo;

import com.mybatis_demo.domain.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取链接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "root", "root");
        //准备sql
        //获取prepareStatement
        PreparedStatement statement = connection.prepareStatement("select * from user ");
        //执行sql  --> 获取结果集 resultSet
        ResultSet resultSet = statement.executeQuery();
        List<User> users = new ArrayList<>();
        //-----------------------------------------------
        ResultSetMetaData metaData = resultSet.getMetaData();
        //列数
        int columnCount = metaData.getColumnCount();
        //索引从1开始
        for (int i = 1; i <= columnCount; i++) {
            System.out.print("第" + i + "列---->");
            String columnTypeName = metaData.getColumnTypeName(i);
            System.out.print("列类型名：" + columnTypeName + " ");
            String columnName = metaData.getColumnName(i);
            System.out.print("列名："+columnName+" ");
            String columnLabel = metaData.getColumnLabel(i);
            System.out.println("列的标识："+columnLabel);
        }
        //-----------------------------------------------
        //处理结果
        while (resultSet.next()) {
            User user = new User();
            Object id = resultSet.getObject("id");
            user.setId((Integer) id);
            Object username = resultSet.getObject("username");
            user.setUsername((String) username);
            Object gender = resultSet.getObject("gender");
            user.setGender((String) gender);
            Object birthday = resultSet.getObject("birthday");
            user.setBirthday((java.util.Date) birthday);
            Object address = resultSet.getObject("address");
            user.setAddress((String) address);
            users.add(user);
        }
        //关闭资源
        resultSet.close();
        statement.close();
        connection.close();

        System.out.println(users);
    }
}
